var app = getApp();
Page({
  data: {
   
  },
  onLoad: function (e) {
    var that = this;
    if (e){
      that.setData({
        jid: e.jid ? e.jid:'',
        id: e.id ? e.id:''
      })
    }
    if (e && e.enterfrom) {
      var enterfrom = e.enterfrom == 'share' ? true : false;
      that.setData({
        enterfrom: enterfrom
      })
    }
    that.requestShareinfo();
  },
  requestShareinfo:function(){
    var that = this;
    //发起请求，获取列表列表
    var id = that.data.id;
    var jid = that.data.jid;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_bargain_share_info',
        activityId: id,
        jid: jid
      },
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          var shareInfo = res.data.data;
          that.setData({
            shareInfo: shareInfo,
            shareDesc: shareInfo.shareDesc,
            shareImg: shareInfo.shareImg
          })
          console.log(shareInfo);
          wx.downloadFile({
            url: shareInfo.shareImg,
            success: function (res1) {
              if (res1.statusCode == 200) {
                console.log(res1.tempFilePath);
                shareInfo.shareImg = res1.tempFilePath;
                wx.downloadFile({
                  url: shareInfo.shareQrcode,
                  success: function (res2) {
                    console.log(res2);
                    if (res2.statusCode == 200) {
                      console.log(res2.tempFilePath);
                      shareInfo.shareQrcode = res2.tempFilePath;
                      that.setData({
                        shareInfo: shareInfo
                      });
                      console.log(that.data.shareInfo)
                      that.createCanvas();
                    }else{
                      that.setData({
                        isDrawfinish:false,
                        tips:"商品小程序码不存在"
                      })
                      wx.hideLoading();
                    }
                  },
                  fail: function () {
                    console.log("下载第二张图片失败")
                    that.setData({
                      isDrawfinish: false,
                      tips: "商品小程序码下载失败"
                    })
                    wx.hideLoading();
                  }
                })
              }else{
                that.setData({
                  isDrawfinish: false,
                  tips: "商品图片不存在"
                })
                wx.hideLoading();
              }
            },
            fail:function(){
              console.log("下载第一张图片失败")
              that.setData({
                isDrawfinish: false,
                tips: "商品图片下载失败"
              })
              wx.hideLoading();
            }
          })
          
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        // wx.hideLoading();
      }
    });
  },
  createCanvas:function(){
    var that = this;
    var shareInfo = that.data.shareInfo;
    console.log(shareInfo);
    const ctx = wx.createCanvasContext('myCanvas');
    var res = wx.getSystemInfoSync();
    var winW = res.windowWidth;
    var canvasW = winW * 650 / 750;
    var canvasH = winW * 855 / 750;
    that.setData({
      canvasW: canvasW,
      canvasH: canvasH
    })
    var shareDesc = shareInfo.shareDesc;
    var shareDescArr = [];
    var singleTxtnum = Math.floor((canvasW - 30)/15);
    var txtLinenum = Math.ceil(shareDesc.length / singleTxtnum) < 3 ? Math.ceil(shareDesc.length / singleTxtnum):3;
    for (var i = 0; i < txtLinenum;i++){
      var length = shareDescArr.length;
      var txtObj = shareDesc.substring(length * singleTxtnum, length *singleTxtnum+singleTxtnum);
      shareDescArr.push(txtObj);
    }
    shareInfo.shareDesc = shareDescArr;
    console.log(shareDescArr); 
    var imgW = canvasW;
    var imgH = winW * 320 / 750;
    // ctx.drawImage(shareInfo.shareImg, 0, 0, imgW, imgH)

    var x = 0;
    var radius = 4;
    var y = 0;
    var width = imgW;
    var height = imgH;
    ctx.rect(0, 0, canvasW, canvasH)
    ctx.setFillStyle('#ffffff')
    ctx.fill()
    ctx.save()
    ctx.beginPath()
    ctx.moveTo(x + radius, y);
    ctx.lineTo(x + width - radius, y);
    ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
    ctx.lineTo(x + width, y + height - radius);
    ctx.quadraticCurveTo(x + width, y + height, x + width, y + height);
    ctx.lineTo(x + radius, y + height);
    ctx.quadraticCurveTo(x, y + height, x, y + height);
    ctx.lineTo(x, y + radius);
    ctx.quadraticCurveTo(x, y, x + radius, y);
    ctx.closePath();
    ctx.clip()
    ctx.drawImage(shareInfo.shareImg, 0, 0, imgW, imgH)
    ctx.restore()
    ctx.setFontSize(15)
    for (var i = 0; i < shareDescArr.length;i++){
      var textY = (imgH + 30) + i * 15 * 1.5;
      ctx.setFillStyle('#333333');
      ctx.fillText(shareDescArr[i], 20, textY);
    }
    var lineY = imgH + 30 + 3 * 15 * 1.5;
    ctx.moveTo(20, lineY);
    ctx.lineTo(canvasW - 20, lineY);
    ctx.setStrokeStyle('#eeeeee');
    ctx.stroke();
    ctx.setFontSize(13);
    ctx.setFillStyle('#999999');
    ctx.fillText('长按或扫描小程序码', (canvasH - lineY), lineY + 14 +(canvasH - lineY - 30)/2);
    ctx.fillText('帮助好友砍价', (canvasH - lineY), lineY + 30 + (canvasH - lineY - 30) / 2);
    ctx.drawImage(shareInfo.shareQrcode, 20, lineY + 10, (canvasH - lineY - 30), (canvasH - lineY - 30));
    ctx.draw();
    wx.hideLoading();
    that.setData({
      isDrawfinish:true
    })
  },
  saveImage: function () {
    var that = this;
    var canvasW = that.data.canvasW;
    var canvasH = that.data.canvasH;
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: canvasW,
      height: canvasH,
      destWidth: canvasW*2,
      destHeight: canvasH*2,
      canvasId: 'myCanvas',
      fileType:'jpg',
      success: function (res) {
        console.log(res.tempFilePath)
        var tempFilePathShow = res.tempFilePath;
        wx.showLoading({
          title: '正在保存',
          mask: true,
          time: 100000
        })
        wx.saveImageToPhotosAlbum({
          filePath: tempFilePathShow,
          success(res) {
            console.log(res);
            app.errorTip(that, "图片保存成功", 2000);
          },
          fail(f) {
            app.errorTip(that, "图片保存失败", 2000);
          },
          complete() {
            wx.hideLoading();
          }
        })
      }
    })  
  },
  onShareAppMessage: function () {
    var that = this;
    var id = that.data.id;
    var jid = that.data.jId;
    var title = that.data.shareDesc;
    var cover = that.data.shareImg;
    app.getPoint(that);
    return {
      title: title,
      imageUrl: cover,
      path: '/subpages/bargainGoodDetail/bargainGoodDetail?enterfrom=share&id=' + id + '&jid=' + jid
    }
  }
})